package com.lgd.warehousemanagement.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author lgd
 * @Date 2024/11/20 13:11
 *  串口 工具类 用于和串口设备通信
 */
public class SerialPort2 {
    private static final String TAG = "SerialPortComm";
    private static final String SERIAL_PORT_PATH = "/dev/ttyS0"; // 根据你的设备调整串口路径
    private static final int BAUD_RATE = 9600; // 波特率

    private File serialPortFile;
    private FileOutputStream serialPortOutputStream;
    private InputStream serialPortInputStream;
    private SerialPortThread readThread;

    public SerialPort2(Context context) {
        serialPortFile = new File(SERIAL_PORT_PATH);
    }

    public SerialPort2(File file, int baudRate, int i) {
    }

    public boolean open() {
        try {
            serialPortOutputStream = new FileOutputStream(serialPortFile);
            serialPortInputStream = new FileInputStream(serialPortFile);

            // 设置串口参数（需要JNI调用底层C/C++代码）
            // 这里假设Android-SerialPort-API已经为你处理了这部分
            // 创建SerialPort对象（需要Android-SerialPort-API库）
//             SerialPort serialPort = new SerialPort(new File(SERIAL_PORT_PATH), BAUD_RATE, 0);


            readThread = new SerialPortThread();
            readThread.start();

            return true;
        } catch (IOException e) {
            Log.e(TAG, "Error opening serial port", e);
            return false;
        }
    }

    public void close() {
        try {
            if (serialPortInputStream != null) {
                serialPortInputStream.close();
            }
            if (serialPortOutputStream != null) {
                serialPortOutputStream.close();
            }
            if (readThread != null) {
                readThread.interrupt();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error closing serial port", e);
        }
    }

    public void sendData(String data) {
        try {
            if (serialPortOutputStream != null) {
                System.out.println("sendData:发发发发发发" + data);
                serialPortOutputStream.write(data.getBytes());
                serialPortOutputStream.flush();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error sending data", e);
        }
    }

    private class SerialPortThread extends Thread {
        private final Handler handler = new Handler(Looper.getMainLooper());

        @Override
        public void run() {
            byte[] buffer = new byte[1024];
            int size;
            try {
                while ((size = serialPortInputStream.read(buffer)) > 0) {
                    final byte[] data = new byte[size];
                    System.arraycopy(buffer, 0, data, 0, size);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            onDataReceived(data);
                        }
                    });
                }
            } catch (IOException e) {
                Log.e(TAG, "Error reading from serial port", e);
            }
        }

        private void onDataReceived(byte[] data) {
            // 处理接收到的数据
            String receivedData = new String(data);
            Log.d(TAG, "Received data: " + receivedData);
        }
    }
}
